import json
from django.http import HttpResponse
from . import define_info

_info = define_info.res_info


def login_required(view_func):
    # 登录判断装饰器
    def wrapper(request, *view_args, **view_kwargs):

        if request.COOKIES.get("username", None):  # 取不到设置默认值为None
            name = request.COOKIES.get("username", None)
            print('get cookie [{}]'.format(name))

        # 判断用户是否登录,session验证
        if request.session.get("is_login", None):
            name = request.session.get("username", None)
            print('get session username is [{}]'.format(name))
            return view_func(request, *view_args, **view_kwargs)
        else:
            # 用户未登录,返回信息
            _info['msg'] = '还未登录'
            return HttpResponse(json.dumps(_info))
    return wrapper
